package com.ifast.common.utils;

import com.github.qcloudsms.SmsMultiSender;
import com.github.qcloudsms.SmsMultiSenderResult;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.google.common.collect.Lists;
import com.xiaoleilu.hutool.date.DateUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 短信发送工具
 * 未测试,谁用谁测试一下吧
 * @author ShiQiang
 *
 */
public class SmsUtil {
	//群发最大数
	public static int groupMaxSendSize = 200;
	
	/**
	 * 单发短信
	 * @param phoneNumber
	 * @param params
	 * @param templateId 
	 */
	public static SmsSingleSenderResult singleSend(String phoneNumber, List<String> params,
			int templateId){
		
		SmsSingleSenderResult result = new SmsSingleSenderResult();
		
		if(StringUtils.isBlank(phoneNumber)
				|| params == null || params.size() < 0 || templateId < 0){
			GjpLogger.info("缺少发送短信必要参数");
			return result;
		}
		
		GjpLogger.info("单次发送电话号码-->" + phoneNumber);
		GjpLogger.info("参数-->" + params.toString() + "   模板ID：" + templateId);  
		
		try {
			
			SmsSingleSender send = new SmsSingleSender(Const.APP_ID, Const.APP_KEY);
			result = send.sendWithParam(Const.NATION_CODE, phoneNumber, templateId, params.toArray(new String[params.size()]), Const.SIGN,"", "");
			GjpLogger.info("单次发送返回值-->\n" + result.toString());
		
		} catch (Exception e) {
			
			GjpLogger.info("发送短信出现异常");
			GjpLogger.info("电话号码-->" + phoneNumber);
			GjpLogger.info("参数-->" + params.toString() + "   模板ID：" + templateId);
			GjpLogger.error(e.getMessage(), e); 
			
			sendSmsErr("群发短信出现异常");  
			e.printStackTrace(); 
		}
		return result;
	}
	
	
	
	/**
	 * 群发短信
	 * 
	 * @param phoneNumbers  不带国家码的手机号列表
	 * @param params 		模板参数列表
	 * @param templId 		模板 id 
	 */
	public static SmsMultiSenderResult groupSend(ArrayList<String> phoneNumbers, ArrayList<String> params,
			int templId){
		
		SmsMultiSenderResult result = new SmsMultiSenderResult();
		
		if(phoneNumbers == null || phoneNumbers.size() < 1 
				|| params == null || params.size() < 0 || templId < 0){
			GjpLogger.info("缺少发送短信必要参数");
			return result;
		}
		
		GjpLogger.info("全部电话号码-->" + phoneNumbers.toString());
		GjpLogger.info("参数-->" + params.toString() + "   模板ID：" + templId);
		 
		SmsMultiSender smsMultiSender = new SmsMultiSender(Const.APP_ID, Const.APP_KEY);
		
		//固定长度分组
		List<List<String>> phoneLimit = Lists.partition(phoneNumbers, groupMaxSendSize);
		
		for(List<String> onceLists : phoneLimit){ 
			
			try {
				
				GjpLogger.info("群发电话号码-->" + onceLists);
				result = smsMultiSender.sendWithParam(Const.NATION_CODE,(ArrayList<String>)onceLists, templId, params, Const.SIGN,"", "");
				GjpLogger.info("群发返回值-->\n" + result.toString());
			
			} catch (Exception e) {
				
				GjpLogger.info("发送短信出现异常");
				GjpLogger.info("电话号码-->" + onceLists.toString());
				GjpLogger.info("参数-->" + params.toString() + "   模板ID：" + templId);
				GjpLogger.error(e.getMessage(), e); 
				
				sendSmsErr("群发短信出现异常"); 
				e.printStackTrace(); 
			}
		} 
		
		return result;
	}

	/**
	 * 发送短信异常报警 
	 */
	public static void sendSmsErr(String type){
		try {
			// 出现异常给管理员发送短息
			if (Const.SMS_ERR_ALARM) {
				String now = DateUtil.now();
				ArrayList<String> params = new ArrayList<>();
				params.add(now);
				params.add(type);
				groupSend(Const.SMS_ERR_PHONE, params, Const.MessageTemplateId.SEND_SMS_ERR.getValue());
			}
			
		} catch (Exception e2) {
			
			GjpLogger.error(e2.getMessage(), e2);
			e2.printStackTrace();
			
		} 
	}

	//测试
	public static void main(String[] args) {
		try {
			// 用单独的方法获取电话号码
			// ArrayList<String> phoneNumbers = getPhoneNumbers();
			ArrayList<String> phoneNumbers = new ArrayList<>();
			phoneNumbers.add("153548910");
			phoneNumbers.add("153548911");
			// 短信模板参数
			ArrayList<String> params = new ArrayList<>();
			params.add("短信发送人测试2");
			params.add("测试");
			// 群发短信
			SmsMultiSenderResult result = groupSend(phoneNumbers, params, 119561);
			System.out.println(phoneNumbers);
			System.out.println(params);
			System.out.println(result);
		} catch (Exception e) {
			e.printStackTrace();
		} 
	} 
}
